const Topik = require('../model/topik');
const Record = require('../model/record')
const User = require('../model/user')

var topik = new Topik({
  cn: '店铺1',
  ko: '가게',
  nature: '명사',
  note: '',
  newnote: ''
})
/*
topik.save((err, res) => {
    if (err) {
        console.error(err);
    } else {
        console.log(res);
    }
})

Topik.updateOne({ cn: '店铺1' }, { note: 'test' }, (err, res) => {
    if (err) {
        console.error(err);
    } else {
        console.log(res);
    }
}) */

/* // 回调
Topik.find({ cn: '店铺1' }, (err, res) => {
    if (err) {
        console.error(err);
    } else {
        console.log(res);
    }
})

// promise
Topik.find({ cn: '店铺1' }).then((err, res) => {
    if (err) {
        console.error(err);
    } else {
        console.log(res);
    }
}) */

// asyn/await
async function getData() {
  let data = await Topik.find({}).populate('uid');
  if (data) {
    console.log(data);
  }
}

getData();

async function putData() {
  let one = await Topik.findById('5c4feece0483bc56926caa14');
  if (one) {
    var saved = Object.assign({}, JSON.parse(JSON.stringify(one)), {
      note: 'haha'
    });
    var record = Object.assign({}, saved)
    record.origincn = one.cn;
    record.originko = one.ko;
    record.originnote = one.note;
    record.uid = '5c514130c2c5164345a27677'
    record._id = undefined;

    console.log('saved', saved)
    let data = await Topik.findByIdAndUpdate('5c4feece0483bc56926caa14', saved)
    let msg = await new Record(record).save()

    console.log(data, msg)
  }
}
//putData();

/* Topik.deleteOne({ cn: '店铺1' }, (err, res) => {
    if (err) {
        console.error(err);
    } else {
        console.log(res);
    }
}) */

/*
Topik.deleteMany({}, (err, res) => {
  if (err) {
    console.error(err);
  } else {
    console.log(res);
  }
})

Record.deleteMany({}, (err, res) => {
  if (err) {
    console.error(err);
  } else {
    console.log(res);
  }
})
*/